package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.GroupContact;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by shijunting on 15-1-29.
 */
public class AddContactsAdapter extends BaseAdapter {

    private ArrayList<GroupContact> mContactList;
    private Context mContext;
    public HashMap<Integer, Boolean> isSelected;


    public AddContactsAdapter(Context context) {
        this.mContext = context;
        isSelected = new HashMap<Integer, Boolean>();
    }

    public void bindData(ArrayList<GroupContact> list){
        this.mContactList = list;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isHas_tag()) {
                isSelected.put(i, true);
            } else {
                isSelected.put(i, false);
            }
        }
    }

    @Override
    public int getCount() {
        return mContactList.size();
    }

    @Override
    public Object getItem(int position) {
        return mContactList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.add_contacts_item, null);
            holder.add_contact_name = (TextView) convertView.findViewById(R.id.add_contact_name);
            holder.has_tag = (CheckBox) convertView.findViewById(R.id.has_tag);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.add_contact_name.setText(mContactList.get(position).getContact_name());
        if (isSelected.get(position)) {
            holder.has_tag.setChecked(true);
        } else {
            holder.has_tag.setChecked(false);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView add_contact_name;
        CheckBox has_tag;
    }
}
